﻿namespace GreenIsomer.Web.Controls.JQuery {
	using System;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.ComponentModel;

	/// <summary>
	/// Server control used to generate a jQuery UI Themed Symbol.
	/// </summary>
	public class ThemedIcon : JQueryWebControl {


		#region Constants: Property defaults

		private const Symbol defaultSymbol = Symbol.None;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the GreenIsomer.Web.Controls.JQuery.ThemedIcon class.
		/// </summary>
		public ThemedIcon() : base(HtmlTextWriterTag.Span) { }

		#endregion


		#region Properties

		/// <summary>
		/// Gets or sets the standard jQuery UI themed symbol to render.
		/// </summary>
		[DefaultValue(defaultSymbol), Category("Appearance")]
		public virtual Symbol Symbol {
			get { return ViewState.GetValue("Symbol", defaultSymbol); }
			set {
				if(!Enum.IsDefined(typeof(Symbol), value)) throw new ArgumentException("Value not defined.", "value");
				ViewState["Symbol"] = value;
			}
		}

		#endregion


		#region Methods: Render hooks

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The GreenIsomer.Web.Controls.JQuery.Manager used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected override void Register(Manager manager) {

			manager.RegisterStyleFile(Settings.Default.UiCoreStyleFile);
			manager.RegisterThemeStyleFile();
		}

		/// <summary>
		/// Sends server control content to a provided System.Web.UI.HtmlTextWriter object, which writes the content to be rendered to the client.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="writer">A System.Web.UI.HtmlTextWriter that represents the output stream to render HTML content on the client.</param>
		public override void RenderControl(HtmlTextWriter writer) {

			string symbolCssClass = Symbol.GetCssClass();
			if(string.IsNullOrEmpty(symbolCssClass)) return;

			symbolCssClass = "ui-icon " + symbolCssClass;

			string cssClass = CssClass;
			CssClass = string.IsNullOrEmpty(cssClass) ? symbolCssClass : cssClass + " " + symbolCssClass;

			if (!MasterUseCssStyling) {
				writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "inline");
				writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "1px");
				writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "15px");
				writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, "15px");
				writer.AddStyleAttribute("zoom", "1");
			}

			base.RenderControl(writer);
		}

		#endregion


	}

}